<template>
  <!-- 器具表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="500"
    draggable
    @close="closeDialog"
    destroy-on-close
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="器具类型编号" prop="assetTypeCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.assetTypeCode"
          placeholder="请输入器具类型编号"
          clearable
          style="width: 320px"
        />

        <span v-else>{{ formData.assetTypeCode }}</span>
      </el-form-item>
      <el-form-item label="器具类型名称" prop="assetTypeName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.assetTypeName"
          placeholder="请输入器具类型名称"
          clearable
          style="width: 320px"
        />

        <span v-else>{{ formData.assetTypeName }}</span>
      </el-form-item>
      <el-form-item label="器具规格" prop="assetSpecification">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.assetSpecification"
          placeholder="请输入器具规格"
          clearable
          style="width: 320px"
        />

        <span v-else>{{ formData.assetSpecification }}</span>
      </el-form-item>
      <!-- <el-form-item label="排序" prop="sort">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.sort"
          placeholder="请输入排序"
          clearable
          style="width: 200px"
        />

        <span v-else>{{ formData.sort }}</span>
      </el-form-item> -->
      <el-form-item label="启用状态" prop="enabled">
        <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>

        <span v-else>{{ formData.enabled == 1 ? "启用" : "停用" }}</span>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
          style="width: 320px"
        />

        <span v-else>{{ formData.remarks }}</span>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="props.isReadonly">
        <span>{{ formData.createTime }}</span>
      </el-form-item>
      <el-form-item
        label="创建人"
        prop="createdUserName"
        v-if="props.isReadonly"
      >
        <span>{{ formData.createdUserName }}</span>
      </el-form-item>
      <el-form-item
        label="更新时间"
        prop="lastUpdateTime"
        v-if="props.isReadonly"
      >
        <span>{{ formData.lastUpdateTime }}</span>
      </el-form-item>
      <el-form-item
        label="更新人"
        prop="lastUpdatedUserName"
        v-if="props.isReadonly"
      >
        <span>{{ formData.lastUpdatedUserName }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EAMMTASSETTYPEEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  parentId: {
    type: String,
    default: () => {
      return "";
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  assetTypeCode: [
    { required: true, message: "请输入器具类型编号", trigger: "blur" },
    {
      required: true,
      pattern: "[^ \x20]+",
      message: "请输入器具类型编码",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  assetTypeName: [
    { required: true, message: "请输入器具类型名称", trigger: "blur" },
    {
      required: true,
      pattern: "[^ \x20]+",
      message: "请输入器具类型名称",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  assetSpecification: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sort: [
    { required: true, message: "请输入排序", trigger: "blur" },
    // {
    //   required: true,
    //   pattern: "[^ \x20]+",
    //   message: "请输入排序",
    //   trigger: "blur",
    // },
    // { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
    {
      validator: validateDegree,
      trigger: "blur",
    },
  ],
  enabled: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});
function validateDegree(rule: any, value: any, callback: (a?: any) => {}) {
  if (isNaN(parseFloat(value)) && isFinite(value)) {
    callback(new Error("请输入整数"));
  } else if (!/^[+-]?\d+?(e[+-]?\d+)?$/.test(value)) {
    callback(new Error("请输入整数"));
  } else if (Number(value) < 0) {
    callback(new Error("请输入大于0的整数"));
  } else {
    callback();
  }
}
const formRef = ref(ElForm);

let formData: any = reactive({
  id: "",
  version: "",
  assetTypeCode: "",
  assetTypeName: "",
  assetSpecification: "",
  parentTypeId: "",
  allId: "",
  allCode: "",
  allName: "",
  sort: "",
  enabled: 1,
  parentId: "",
  remarks: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        assetTypeCode,
        assetTypeName,
        assetSpecification,
        parentTypeId,
        allId,
        allCode,
        allName,
        sort,
        enabled,
        parentId,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        assetTypeCode,
        assetTypeName,
        assetSpecification,
        parentTypeId,
        allId,
        allCode,
        allName,
        sort,
        enabled,
        parentId,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();

  formData.id = "";
  formData.version = "";
  formData.assetTypeCode = "";
  formData.assetTypeName = "";
  formData.assetSpecification = "";
  formData.parentTypeId = "";
  formData.allId = "";
  formData.allCode = "";
  formData.allName = "";
  formData.sort = "";
  formData.enabled = 1;
  formData.parentId = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

const parentIdDialog = ref("");
watch(
  () => props.parentId,
  (newVal: string) => {
    parentIdDialog.value = newVal;
  }
);
const isReadonlyDialog = ref(false);
watch(
  () => props.isReadonly,
  (newVal: boolean) => {
    isReadonlyDialog.value = newVal;
  }
);
/** 设备种类保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      // 添加子机构
      const text = ref("");
      if (parentIdDialog.value) {
        formData.parentTypeId = parentIdDialog.value;
        loading.value = true;
        text.value = "添加子机构";
      } else if (formData.id) {
        loading.value = true;
        text.value = "编辑";
      } else {
        loading.value = true;
        formData.parentTypeId = "0";
        text.value = "新增";
      }
      loading.value = false;
      // isReadonlyDialog.value = false;
      await request({
        url: "/eam/mtAssetType/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text.value + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
